<?php
namespace Backend\Controller;
use Think\Controller;

/**
 * 代码位管理控制器
 */
class CodeController extends BaseController
{
    public function index(){

        $model =D('Code');
        $data=$model ->get_code();

        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));

		$this->display('index');
    }

    public  function  create(){
        $data=$_POST;
        //var_dump($data);exit;
        if($data)
        {
            $model = D('Code');
            if (!$data = $model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError()]);
                exit;
            }else{

                if($result = $model->add())
                {

                    $this->ajaxReturn(['status' => 'success', 'msg' => '添加成功！']);
                    exit;
                }else{
                    $this->ajaxReturn(['status' => 'failed', 'msg' =>$model->getError() ]);
                    exit;
                }
            }
        }
        $result =D('Media')->field('id,name')->order('id desc')->select();
        $this->assign('result',$result);

//        $group =D('CodeGroup')->field('id,group_name')->select();
//        $this->assign('group',$group);

        $this->display();
    }

    public  function MediaTogroup(){
        $media_id =I('post.id');

        $model =D('CodeGroup');
        if($media_id){
            $group = $model->field('id,group_name')->where(array("media_id" => $media_id))->order('id desc')->select();
            $str='';
            $str.='<option value="">'.'请选择'.'</option>';
            foreach ($group as  $k=>$v){

                $str.='<option value=" '.$v['id'].'">'.$v['group_name'].'</option>';
            }

            $this->ajaxReturn(['codegroup'=>$str,'status' => 'success', 'msg' => '成功！']);
            exit;
        }else{

            $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            exit;
        }

    }

    //媒体联动代码位
    public  function MediaTocode(){
        $media_id =I('post.id');

        $model =D('Code');
        if($media_id){
            $code = $model->field('id,code_name')->where(array("media_id" => $media_id))->order('id desc')->select();
            $str='';
            $str.='<option value="">'.'请选择代码位...'.'</option>';
            foreach ($code as  $k=>$v){

                $str.='<option value=" '.$v['id'].'">'.$v['code_name'].'</option>';
            }

            $this->ajaxReturn(['code'=>$str,'status' => 'success', 'msg' => '成功！']);
            exit;
        }else{

            $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            exit;
        }

    }

    public  function  edit(){
        $id = I('get.id');
        $data=$_POST;

        if($data)
        {
            //var_dump($data);exit;
            $model = D('Code');
            if (!$model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            }else{

                if($model->save() !== FALSE)
                {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '修改成功！']);
                    exit;

                }else{
                    $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
                    exit;
                }
            }
        }
            $where['id'] = ['eq', $id];
            //取要修改的数据
            $code = D('Code')->where($where)->find();
            //var_export($code);exit;
            $this->assign('code', $code);

            //取代码位分组
            $group = D('CodeGroup')->field('id,group_name')->where(array('media_id'=>$code['media_id']))->select();
             // var_export($group);exit;
            $this->assign( 'group', $group);
            //取媒体类型
            $result =D('Media')->field('id,name')->order('id desc')->select();
            $this->assign('result',$result);
            $this->display();


    }

    public  function  delete(){
        if(!$_POST['id']){
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作有误，重新选择！']);
            die;

        }
        $where ='id in('.trim($_POST['id'],',').')';
        $model = D('Code');
        $res =$model->where($where)->delete();
        //echo $model->getLastSql();exit;
        if ($res) {
            $this->ajaxReturn(['status' => 'success', 'msg' => '操作成功！']);
        } else {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作失败！']);
        }
    }


}